package com.atyun.gaojin.notify.consoleNotify;

import com.atyun.gaojin.notify.AlterNotify;
import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * 告警通知简单实现类
 * @Author: 云水瑶
 * @Date: 2025/5/1
*/
@Slf4j
public class AlterNotifyConsoleImpl implements AlterNotify {
    @Override
    public void sendAlterNotify(String poolName, Runnable command, ThreadPoolExecutor executor) {
        log.error("线程{}开始打印告警通知\n", poolName);
        System.err.printf("[线程池告警] 线程池[%s]任务被拒绝！\n" +
                        "活跃线程: %d/%d\n" +
                        "队列: %d/%d\n" +
                        "任务类型: %s\n",
                poolName,
                executor.getActiveCount(), executor.getMaximumPoolSize(),
                executor.getQueue().size(), executor.getQueue().size() + executor.getQueue().remainingCapacity(),
                command.getClass().getSimpleName());
    }

    @Override
    public String shouldAlterNotify(String poolName, Runnable command, ThreadPoolExecutor executor) {
        String content = "线程池告警！！！线程池" + poolName +
                "任务处理被拒绝\n活跃线程：" + executor.getActiveCount() + "/" + executor.getMaximumPoolSize() +
                "\n队列：" + executor.getQueue().size() + "/" + (executor.getQueue().size() + executor.getQueue().remainingCapacity()) +
                "\n任务类型：" + command.getClass().getSimpleName();
        return content;
    }
}
